/*
 * 0690. 阶乘
 * 给一个数字 n, 以字符串的形式返回数字的阶乘
 * https://www.lintcode.com/problem/factorial/description
 * 
 * 样例
 * 给一个数字 n = 20
 * 返回 2432902008176640000
 * 
 * 2018.06.28 @jeyming
 */
package factorial_0690;

public class Factorial_0690 {
    /**
     * @param n: an integer
     * @return:  the factorial of n
     */
    public String factorial(int n) {
        // write your code here
    	String res = "";
    	if(n == 0){
            return "1";
        }
    	for(int i = 1; i <= n; ++i) {
    		if(i == 1) {
    			res += i;
    		} else {
    			int sum = res.length();
    			int cnt = res.length() - 1;
    			String tmp = "";
    			int num = 0;
    			while((cnt >= 0) || (num > 0)) {
    				if(cnt >= 0) {
    					num += (res.charAt(cnt--) - '0') * i;
    				}
    				//tmp += num % 10;
    				tmp = num % 10 + tmp;
    				//res += num % 10;
					num /= 10;
    			}
//    			res = res.substring(sum, res.length());
    			res = tmp;
//    			res = "";
//    			for(int j = tmp.length() - 1; j >= 0; --j) {
//    				res += tmp.charAt(j);
//    			}
    		}
    	}
    	return res;
    }

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
